﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "InstanceJoinTransactionEventHandler.h"
#include "IDocument.h"
#include "ITransactionEventArgs.h"
#include "ITransactionEvent.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

gcmp::InstanceJoinTransactionEventHandler::InstanceJoinTransactionEventHandler()
{

}

gcmp::InstanceJoinTransactionEventHandler::~InstanceJoinTransactionEventHandler()
{

}

gcmp::InstanceJoinTransactionEventHandler* gcmp::InstanceJoinTransactionEventHandler::Get()
{
    static InstanceJoinTransactionEventHandler transactionEventHandler;
    return &transactionEventHandler;
}

void gcmp::InstanceJoinTransactionEventHandler::On(ITransactionEventArgs* pArgs)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pArgs, L"pArgs为空",L"GDMPLab",L"2024-03-30");
    TransactionEventType type = pArgs->GetType();
    const ITransactionEvent* pEvent = quick_cast<const ITransactionEvent>(pArgs->GetEvent());
    DBG_WARN_AND_RETURN_VOID_UNLESS(pEvent, L"pEvent为空",L"GDMPLab",L"2024-03-30");

    IDocument* pDoc = pEvent->GetSender();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    RuntimeId docId = pDoc->GetRuntimeId();

    if (type == TransactionEventType::PreCommit)
    {
        const std::set<ElementId> &elementIds = GetNeedDeleteElementIdsAfterCommit(docId);
        if (!elementIds.empty())
        {
            FOR_EACH(id, elementIds)
            {
                pDoc->DeleteElement(id);
            }
            ClearAllNeedDeleteElementIdsAfterCommit(docId);
        }
       
    }
    else if (type == TransactionEventType::PostRollBack)
    {
        ClearAllNeedDeleteElementIdsAfterCommit(docId);
    }

}

bool gcmp::InstanceJoinTransactionEventHandler::AddNeedDeleteElementIdAfterCommit(RuntimeId docId, const ElementId &id)
{
   return m_docToneedDeleteElementIdsAfterCommit[docId].insert(id).second;
}

bool gcmp::InstanceJoinTransactionEventHandler::RemoveNeedDeleteElementIdAfterCommit(RuntimeId docId, const ElementId &id)
{
    auto iter = m_docToneedDeleteElementIdsAfterCommit.find(docId);
    if (iter != m_docToneedDeleteElementIdsAfterCommit.end())
    {
        return m_docToneedDeleteElementIdsAfterCommit[docId].erase(id) > 0;
    }
    DBG_WARN(L"docId不存在该文档",L"GDMPLab",L"2024-03-30");
    return false;
}

std::set<gcmp::ElementId> gcmp::InstanceJoinTransactionEventHandler::GetNeedDeleteElementIdsAfterCommit(RuntimeId docId) const
{
    auto iter = m_docToneedDeleteElementIdsAfterCommit.find(docId);
    if (iter != m_docToneedDeleteElementIdsAfterCommit.end())
    {
        return m_docToneedDeleteElementIdsAfterCommit.at(docId);
    }
    std::set<gcmp::ElementId> res;
    return res;
}

void gcmp::InstanceJoinTransactionEventHandler::ClearAllNeedDeleteElementIdsAfterCommit(RuntimeId docId)
{
    auto iter = m_docToneedDeleteElementIdsAfterCommit.find(docId);
    if (iter != m_docToneedDeleteElementIdsAfterCommit.end())
    {
        return m_docToneedDeleteElementIdsAfterCommit.at(docId).clear();
    }
    return ;
}
